package com.jsgt.greekfire.ui.trading.buycoin

import android.content.Context
import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.CoinBean
import com.jsgt.greekfire.event.TradingEvent
import com.jsgt.greekfire.factory.CoinViewModelFactory
import com.jsgt.greekfire.viewmodel.CoinViewModel
import kotlinx.android.synthetic.main.activity_buy_coin.*
import org.greenrobot.eventbus.EventBus

/**
 * 购买UTI
 * author fhx
 * create time 2020/4/24 9:06
 **/
class BuyCoinActivity : BaseActivity() {
    private val coinViewModel: CoinViewModel by lazy {
        ViewModelProvider(
            this,
            CoinViewModelFactory()
        ).get(CoinViewModel::class.java)
    }
    private var tradingBean: CoinBean? = null

    companion object {
        private const val TRADING_BEAN = "bean"
        fun start(context: Context, coinBean: CoinBean) {
            val intent = Intent(context, BuyCoinActivity::class.java)
            intent.putExtra(TRADING_BEAN, coinBean)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_buy_coin

    override fun main() {
        title = getString(R.string.buy_uti)
        tradingBean = intent.getParcelableExtra(TRADING_BEAN)
//        usdt_price_tv.text = "USDT ${tradingBean!!.assetPrice.toString()}/UTI"
        all_num_tv.setOnClickListener {
//            buy_num_et.setText(tradingBean!!.totalAmt.toString())
        }
        buy_num_et.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                if (!TextUtils.isEmpty(s)) {
//                    calcu_sum_tv.text = "${s.toString().toFloat() * tradingBean!!.assetPrice!!.toFloat()}"
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

        })
        buy_tv.setOnClickListener {
            buy()
        }
        coinViewModel.buyCoinLiveData.observe(this, Observer {
            ToastUtils.show(this, "购买成功")
            EventBus.getDefault().post(TradingEvent())
            finish()
        })
    }

    private fun buy() {
        val num = buy_num_et.text.toString().trim()
        if (TextUtils.isEmpty(num)) {
            ToastUtils.show(this, "请输入数量")
            return
        }
//        if (num.toFloat() > tradingBean!!.totalAmt!!.toFloat()) {
//            ToastUtils.show(this, "数量有误")
//            return
//        }
//        coinViewModel.buyCoin(
//            tradingBean!!.userGuid!!,
//            tradingBean!!.guid!!,
//            "2",
//            tradingBean!!.assetPrice.toString(),
//            num,
//            tradingBean!!.assetGuid!!,
//            "0.1"
//        )
    }
}